package com.manager.customer.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.result.Result;
import com.manager.customer.listener.CustomerListener;
import com.manager.customer.mapper.CustomerMapper;
import com.manager.customer.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manager.dto.CustomerDto;
import com.manager.model.Customer;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    //分页查询客户信息
    @Override
    public Result<Page> getPage(int page, int pageSize, Integer id, String name,String city) {
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper();
        if (id != null){
            wrapper.eq(Customer::getId,id);
        }
        if (name != null){
            wrapper.like(Customer::getName,name);
        }
        if (city != null){
            wrapper.like(Customer::getCity,city);
        }
        //添加排序条件
        wrapper.orderByAsc(Customer::getId);
        //执行查询
        this.page(pageInfo,wrapper);
        return Result.ok(pageInfo);
    }
    //批量删除客户
    @Override
    public Result<String> deleteSellers(List<Integer> ids) {
        this.removeByIds(ids);
        return Result.ok("删除成功");
    }
    //导入客户信息
    @Override
    public void importCustomer(MultipartFile file, Date beginTime, Date endTime) {
        try {
            EasyExcel.read(file.getInputStream(),CustomerDto.class,new CustomerListener(baseMapper,beginTime,endTime)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //导出客户信息
    @Override
    public void exportCustomer(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("客户数据", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Customer> customerList = customerMapper.selectList(null);
            List<CustomerDto> customerDtoList = new ArrayList<>(customerList.size());
            for(Customer customer : customerList) {
                CustomerDto customerDto = new CustomerDto();
                BeanUtils.copyProperties(customer,customerDto);
                customerDtoList.add(customerDto);
            }
            EasyExcel.write(response.getOutputStream(), CustomerDto.class).sheet("客户数据").doWrite(customerDtoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
